from spass.core.message import message
from spass.api.api_db import search_user
from spass.core.error import error
from optparse import OptionParser

def callback_user(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    users = search_user(parser.values.user[0], parser.values.user[1])
    message('nb.users.found', [str(len(users))])
    for user in users:
        message('user.identity', [str(user.id), user.fullname, user.name])
    return 0

def search(argv):
    if len(argv) < 1:
        error('args.need', ['search', ''])
        return 1

    parser = OptionParser()
    parser.set_usage("""./spass search <options>...""")
    parser.add_option("--user", action="callback", callback=callback_user, nargs=2, type="string", dest="user",
                      help="Search an user from a regex string. ARG : <first name string> <second name string>")
    (options, args) = parser.parse_args(argv)
    return 0
